﻿using System.Linq;
using PSE.Deployment.SetupBase;
using PSE.Deployment.SetupBase.Parameter.Enums;
using PSE.Deployment.SetupBase.UI;
using PSE.Deployment.SetupBase.UI.CustomComponents;
using PSE.Deployment.Util;
using Sinacor.Deployment.Setup.Main;

namespace Sinacor.Deployment.Setup.UI
{
    public partial class ComponentsSelection : BaseWizardWindow
    {
        public bool ShowWindow { get; set; }

        public ComponentsSelection()
        {
            this.InitializeComponent();

            CheckPrerequisites();

            SetAllowNext();

            treeview.CheckStateChanging += new GroupsTreeView.CheckedChangingEventHandler(treeview_CheckStateChanging);
            treeview.CheckStateChanged += new GroupsTreeView.CheckedChangedEventHandler(treeview_CheckStateChanged);

            ShowWindow = treeview.Groups.Count() > 1;
        }

        void treeview_CheckStateChanged(object sender, CheckedChangedEventArgs e)
        {
            SetAllowNext();
        }

        private void treeview_CheckStateChanging(object sender, CheckedChangingEventArgs e)
        {
            if (sender is TreeViewModel)
            {
                TreeViewModel tvm = (TreeViewModel)sender;

                if (tvm.Id.Equals("ServiceLayer") && (e.IsChecked == null || e.IsChecked.GetValueOrDefault()))
                {
                    TreeViewModel msmq = treeview.TreeViewModels.Find(m => m.Id == "MSMQ");
                    if (msmq != null)
                        msmq.IsChecked = true;
                }
                else if (tvm.Id.Equals("MSMQ"))
                {
                    TreeViewModel sm = treeview.TreeViewModels.Find(m => m.Id == "ServiceLayer");
                    e.Cancel = (sm != null && (sm.IsChecked == null || sm.IsChecked.GetValueOrDefault()));
                }
            }
        }

        private void CheckPrerequisites()
        {
            if (SetupMainFactory.GetInstance<SinacorMain>().Parameter.Prerequisites.DataBase != PrerequisitesStatus.Ok ||
                SetupMainFactory.GetInstance<SinacorMain>().Parameter.Prerequisites.PSEComponentsServer != PrerequisitesStatus.Ok)
            {
                TreeViewModel database = treeview.TreeViewModels.Find(t => t.Id == "Database");

                if (database != null)
                {
                    database.IsEnabled = false;
                    database.IsChecked = false;
                    database.Children.SelectDeep(t => t.Children).ToList().ForEach(c => c.IsEnabled = false);
                }

                TreeViewModel service = treeview.TreeViewModels.Find(t => t.Id == "ServiceLayer");

                if (service != null)
                {
                    service.IsEnabled = false;
                    service.IsChecked = false;
                    service.Children.SelectDeep(t => t.Children).ToList().ForEach(c => c.IsEnabled = false);
                }

                TreeViewModel pm = treeview.TreeViewModels.Find(t => t.Id == "ProcessManager");

                if (pm != null)
                {
                    pm.IsEnabled = false;
                    pm.IsChecked = false;
                    pm.Children.SelectDeep(t => t.Children).ToList().ForEach(c => c.IsEnabled = false);
                }
            }

            if (SetupMainFactory.GetInstance<SinacorMain>().Parameter.Prerequisites.PSEComponentsClient != PrerequisitesStatus.Ok)
            {
                TreeViewModel interfaceLayer = treeview.TreeViewModels.Find(t => t.Id == "InterfaceLayer");

                if (interfaceLayer != null)
                {
                    interfaceLayer.IsEnabled = false;
                    interfaceLayer.IsChecked = false;
                    interfaceLayer.Children.SelectDeep(t => t.Children).ToList().ForEach(c => c.IsEnabled = false);
                }
            }
        }

        private void SetAllowNext()
        {
            this.AllowNext = treeview.Groups.Any(g => (g.Install == null || g.Install.GetValueOrDefault()) && !g.Installed) ||
                             treeview.Groups.Any(g => (g.Install.HasValue && !g.Install.Value) && g.Installed);
        }
    }
}